package com.tool.toollibrary.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tool.toollibrary.R;
import com.tool.toollibrary.utils.FileUtils;
import com.tool.toollibrary.base.BaseActivity;
import com.tool.mylibrary.utils.Logger;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import top.zibin.luban.CompressionPredicate;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;


/**原来压缩测试
 * Created by Google on 2018/6/7.
 */

public class CompressTestActivity extends BaseActivity {
    private static final String TAG = "CompressTestActivity";
    @BindView(R.id.compress)
    Button compress;
    @BindView(R.id.old_image)
    ImageView oldImage;
    @BindView(R.id.new_image)
    ImageView newImage;
    @BindView(R.id.sd_test)
    Button sd_test;
    @BindView(R.id.content_des)
    TextView content_des;
    @BindView(R.id.et_quality)
    EditText et_quality;
    private int quality = 90;


    @Override
    public int getLayoutId() {
        return R.layout.compress_layout;
    }

    @Override
    public void initView() {
        final String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
        final String oldPath = path + "/earth.jpg";
        ButterKnife.bind(this);
        final File oldFile = new File(oldPath);
        final long oldSize = oldFile.length() / 1024;
        compress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = et_quality.getText().toString().trim();
                if (!TextUtils.isEmpty(trim)){
                    quality=Integer.valueOf(trim);
                }
                String imagePath = FileUtils.compressImage(oldPath, path + "/compress.jpg", quality);

                File newFile = new File(imagePath);
                String absolutePath = newFile.getAbsolutePath();

                long newSize = newFile.length() / 1024;
                long m =  oldSize/newSize;
                content_des.setText("压缩质量"+quality+"\n"+"压缩前大小" + oldSize + "kb" + "\n" + "压缩后大小" + newSize + "kb" + "\n" +
                        "压缩后占压缩前比例" + m + "\n");
                Logger.e("图片地址", newFile.getAbsolutePath());
                Logger.e("压缩前图片大小", "" + oldFile.length() / 1024 + "kb");
                Logger.e("压缩后图片大小", newFile.length() / 1024 + "kb");
//                Bitmap bitmap = BitmapFactory.decodeFile(oldPath);
                Bitmap bitmap1 = BitmapFactory.decodeFile(imagePath);
//                int degree = FileUtils.readPictureDegree(oldPath);//获取相片拍摄角度
//                if (degree != 0) {//旋转照片角度，防止头像横着显示
//                    bitmap = FileUtils.rotateBitmap(bitmap, degree);
//                }
//                oldImage.setImageBitmap(bitmap);
                newImage.setImageBitmap(bitmap1);
            }
        });

        sd_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                testStorge();
                Luban.with(CompressTestActivity.this)
                        .load(new File((path+"/earth.jpg")))
                        .ignoreBy(100)
                        .setTargetDir((path + "/"))
                        .filter(new CompressionPredicate() {
                            @Override
                            public boolean apply(String path) {
                                return !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif"));
                            }
                        })
                        .setCompressListener(new OnCompressListener() {
                            @Override
                            public void onStart() {
                                // TODO 压缩开始前调用，可以在方法内启动 loading UI
                                Toast.makeText(CompressTestActivity.this,"开始压缩", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onSuccess(File file) {
                                // TODO 压缩成功后调用，返回压缩后的图片文件
                                Toast.makeText(CompressTestActivity.this,"压缩完成", Toast.LENGTH_SHORT).show();
                                Bitmap bitmap1 = BitmapFactory.decodeFile(file.getAbsolutePath());
                                newImage.setImageBitmap(bitmap1);
                                long newSize = file.length() / 1024;
                                content_des.setText("压缩质量"+quality+"\n"+"压缩前大小" + oldSize + "kb" + "\n" + "压缩后大小" + file.length()/1024 + "kb" + "\n" +
                                        "压缩后占压缩前比例" + oldSize/newSize + "\n");
                            }

                            @Override
                            public void onError(Throwable e) {
                                // TODO 当压缩过程出现问题时调用
                                Toast.makeText(CompressTestActivity.this,"压缩失败", Toast.LENGTH_SHORT).show();

                            }
                        }).launch();
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void processClick(View v) {

    }

    private void testStorge() {
        //1.返回结果： /data
        Log.d(TAG, Environment.getDataDirectory().toString());

        //2.返回结果： /cache
        Log.d(TAG, Environment.getDownloadCacheDirectory().toString());

        //3.返回结果： /storage/emulated/0 外部存储的根目录
        Log.d(TAG, Environment.getExternalStorageDirectory().toString());

        //4.返回结果：mounted，表示是外部存储
        Log.d(TAG, Environment.getExternalStorageState().toString());

        //5.返回结果:mounted
        Log.d(TAG, Environment.getExternalStorageState(new File(Environment.getExternalStorageDirectory(), "demo.png")).toString());

        //6.返回结果:/system
        Log.d(TAG, Environment.getRootDirectory().toString());

        //7.返回结果:false 表示外部存储不可安装apk
        Log.d(TAG, Environment.isExternalStorageEmulated() + "");

        //8.返回结果:false 表示外部存储不可安装apk
        Log.d(TAG, Environment.isExternalStorageEmulated(new File(Environment.getExternalStorageDirectory(), "demo.png")) + "");//png存不存在不影响结果

        //9.返回结果:false 表示是内置内存卡
        Log.d(TAG, Environment.isExternalStorageRemovable() + "");

        //10.返回结果:false 表示是内置内存卡
        Log.d(TAG, Environment.isExternalStorageRemovable(new File(Environment.getExternalStorageDirectory(), "demo.png")) + "");

        /**
         * 十大公共目录
         */
        //11-1.返回结果：storage/emulated/0/Music
        Log.d(TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).toString());

        //11-2.返回结果：storage/emulated/0/Pictures
        Log.d(TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString());

        //11-3.返回结果：storage/emulated/0/Alarms
        Log.d(TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS).toString());

        //11-4.返回结果：storage/emulated/0/DCIM
        Log.d(TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString());

        //11-5.返回结果：storage/emulated/0/Documents
        Log.d(TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).toString());

        //11-6.返回结果：storage/emulated/0/Download
        Log.d(TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());

        //11-7.返回结果：storage/emulated/0/Movies
        Log.d(TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).toString());

        //11-8.返回结果：storage/emulated/0/Notifications
        Log.d(TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS).toString());

        //11-9.返回结果：storage/emulated/0/Podcasts
        Log.d(TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS).toString());

        //11-10.返回结果：storage/emulated/0/Ringtones
        Log.d(TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES).toString());
    }

}
